package com.android.tiango.baba.act;

import java.sql.SQLException;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.BaBaseApplication;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.db.Area;
import com.android.tiango.baba.db.AssetsDatabaseManager;
import com.android.tiango.baba.db.City;
import com.android.tiango.baba.db.DbHelper;
import com.android.tiango.baba.db.Province;
import com.android.tiango.baba.entity.AddressModel;
import com.android.tiango.baba.entity.addressEnity2;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.CountdownView;
import com.j256.ormlite.dao.Dao;

/**
 * 确认收货地址
 * 
 * @author suip
 * 
 */
public class ActConfirm extends BaBaseActivity implements OnClickListener {

	private ImageView mEdit;
	private Button mConfirm;
	private CountdownView mCountdownView;
	private String mLogId;
	private TextView mAddress, mName, mPhone;
	private DbHelper helper;
	private String mAddressID;
	
	public final static int FROM_ACTIVE = 0;
	public final static int FROM_BAOBEI = 1;
	private int from_type;
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {

		};
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_confirm);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.DATA)) {
			mLogId = intent.getStringExtra(Constants.DATA);
			from_type = intent.getIntExtra(Constants.TYPE, FROM_ACTIVE);
			initView();
			loadAddress();
		} else {
			ToastUtils.showMessage("Id is null");
		}
		
	}

	private void initView() {
		setTitleName("确认收货地址");
		if (null != mTitleBar) {
			mTitleBar.setBackIcon(R.drawable.back);
			setBackOnClickListener();
		}
		mCountdownView = (CountdownView) findViewById(R.id.cv_CountdownView);
		if(from_type==FROM_ACTIVE){
			mCountdownView.setTime(mHandler, BaBaseApplication.getmServerTime()
					+ (10 * 60 * 1000));
		}else if (from_type==FROM_BAOBEI) {
			
		}
		
		mEdit = (ImageView) findViewById(R.id.iv_edit);
		mConfirm = (Button) findViewById(R.id.bt_confirm);
		mAddress = (TextView) findViewById(R.id.tv_address);
		mName = (TextView) findViewById(R.id.tv_name);
		mPhone = (TextView) findViewById(R.id.tv_phone);
		
		mConfirm.setOnClickListener(this);
		mEdit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_edit:
			showActivity(ActAdressList.class, false);
			break;
		case R.id.bt_confirm:
			if (null != mAddressID) {
				confirmDate(mAddressID);
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		loadAddress();
	}
	private void loadAddress() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());

		client.get(HttpUrl.getUrl(HttpUrl.DEFAULTADDRESS), params,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object ob) {
						dismissWaitingDialog();
						addressEnity2 addressEnity = (addressEnity2) ob;
						if (null != addressEnity) {
							AddressModel addressmodel=addressEnity.getData();
							if(null!=addressmodel){
								mName.setText(StringUtils.getDefaultString(addressmodel.getShipName()));
								mPhone.setText(addressmodel.getPhoneNumber());
								mAddress.setText(initCityCode(Integer
										.valueOf(addressmodel.getAreaId()))
										+ addressmodel.getDetailAddress());
								mAddressID = addressmodel.getAddressId();
							}
						}
						ToastUtils.showMessage("成功了");
					}

					@Override
					public void onHttpStarted() {
						showWaitingDialog("正在加载...");
					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						dismissWaitingDialog();

						if (!StringUtils.isEmpty(arg1)) {
							ToastUtils.showMessage(arg1);
						} else {
							ToastUtils.showMessage("抱歉,请重新尝试");

						}
					}
				},addressEnity2.class);
	}

	// 提交订单
	private void confirmDate(String addressId) {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());
		params.put("orderLogId", mLogId);
		params.put("addressId", addressId);
		client.post(HttpUrl.getUrl(HttpUrl.SUBMITORDER), params,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object ob) {
						finish();
					}

					@Override
					public void onHttpStarted() {
						showWaitingDialog("正在加载...");
					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						dismissWaitingDialog();

						if (!StringUtils.isEmpty(arg1)) {
							ToastUtils.showMessage(arg1);
						} else {
							ToastUtils.showMessage("抱歉,请重新尝试");

						}
					}
				});
	}

	private String initCityCode(int coutyId) {
		AssetsDatabaseManager.initManager(getApplicationContext());
		AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
		mg.getDatabase("ormlite.db");
		helper = new DbHelper(this);
		String arName = "";
		String cityName = "";
		String prName = "";
		try {
			Dao<Province, Integer> dataPro = helper.getProvinceDao();
			Dao<City, Integer> daoCityD = helper.getCityDao();
			Dao<Area, Integer> daoAreaD = helper.getAreaDao();
			List<Area> listArea = daoAreaD.queryForEq("areaId", coutyId);
			if (null != listArea && listArea.size() > 0) {
				Area arBean = listArea.get(0);
				int cityId = arBean.getCityId();
				int prId = arBean.getProvinceId();
				arName = arBean.getAreaName();
				List<City> cityList = daoCityD.queryForEq("cityId", cityId);
				if (null != cityList && cityList.size() > 0) {
					City cityBean = cityList.get(0);
					cityName = cityBean.getCityName();
					List<Province> prList = dataPro.queryForEq("provinceId",
							prId);
					if (null != prList && prList.size() > 0) {
						Province prBean = prList.get(0);
						prName = prBean.getProvinceName();
					}
				}

			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer();
		sb.append(prName);
		//sb.append("  ");
		sb.append(cityName);
		//sb.append("  ");
		sb.append(arName);
		//sb.append("  ");
		return sb.toString();
	}
}
